#ifndef __CPESBuilder_H__
#define __CPESBuilder_H__
#include "IPESBuilder.h"
#include "ITSBuilder.h"
#include "CDynamicArray.h"
#include "CPES.h"
#include "CTS.h"
#include <map>
#include <set>
#include <vector>

/** 
 This class builds PES from TS packets.it assembles all TS packets that belong to the same PES 
 and reconstructs the original PES out of them all.Each finished PES puts into the relevant PES list
 based on the PES PID.
*/
class CPESBuilder : public IPESBuilder
{
public:
	enum EFixMode{
		none,
		allowAny,
		allowFew,
		considerLength
	};
	/** 
    c'tor
   */
	CPESBuilder(std::set<unsigned int> pidsEFixMode, EFixMode eMode = none);
    /** 
    d'tor. 
    deletes the PES lists  and the list that keeps pointers to unfinished PESs
    */
	virtual ~CPESBuilder(void);
    /** 
     Build audio and video PESs from the TSs inside a ITSBuilder
	 @param cTSBuilder - this object contains TS lists
    */
    virtual void buildPes(ITSBuilder* cTSBuilder);

	/**
	Returns a list of PESs
	@param PID - the pid of the stream
	@return a vector that contains all the PESs that belong to this PID
	*/
	virtual std::vector<CPES*>* getPESPackets(unsigned int PID);

private:
	 /** 
     add each finished PES into the relevant PES list.
    */
	void _addTSToPes(unsigned int PID,CTS* CTSpacket);

	std::map<unsigned int, CDynamicArray*> m_cLastPes;
	std::map<unsigned int, int> m_nLastPesCounter;
	std::set<unsigned int> m_pids;
	std::map<unsigned int, std::vector<CPES*>*> m_lPesList;
	EFixMode m_eMode;
};

#endif //__CPESBuilder_H__
